package daiwei.javaWeb.exam.serviceImpl;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import daiwei.javaWeb.exam.dao.Mapper;
import daiwei.javaWeb.exam.meta.BoughtProduct;
import daiwei.javaWeb.exam.meta.Product;
import daiwei.javaWeb.exam.meta.Result;
import daiwei.javaWeb.exam.meta.ShowProduct;
import daiwei.javaWeb.exam.meta.User;
import daiwei.javaWeb.exam.service.DataBaseOP;

@Service("DataBaseOPImpl")
public class DataBaseOPImpl implements DataBaseOP {

	@Autowired
	private Mapper mapper;

	@Override
	public User getUser(String userName, String password) throws Exception {
		return mapper.getUser(userName, password);
	}

	@Override
	public List<Product> getContents() throws Exception {
		return mapper.getContents();
	}
	
	

	@Override
	public List<BoughtProduct> getAccount(int userId) throws Exception {
		return mapper.getBoughtProducts(userId);
	}

	@Override
	public BoughtProduct getBoughtProduct(int userId, int contentId) throws Exception {
		return mapper.getBoughtProduct(userId, contentId);
	}

	@Transactional
	@Override
	public int addProductAndGetLastId(Product p) throws Exception {
		mapper.addProduct(p);
		return mapper.getContentId();
	}

	@Override
	public ShowProduct getShowProduct(int id, HttpSession session) throws Exception {
		ShowProduct show = null;
		Product p = mapper.getContent(id);
		User user = (User) session.getAttribute("user");
		if (user != null && user.getUsertype() == 0) {
			BoughtProduct bp = mapper.getBoughtProduct(user.getId(), id);
			if (bp != null) {
				show = new ShowProduct(id, Double.parseDouble(bp.getPrice()), bp.getTitle(), bp.getImage(), bp.getSummary(), bp.getDetail(),
						bp.getBuyPrice());
				show.setIsBuy(true);
			} else {
				 show = new ShowProduct(id, Double.parseDouble(p.getPrice()), p.getTitle(), p.getImage(), p.getSummary(),
							 p.getDetail(), 0);
			}
		} else {
			show = new ShowProduct(id, Double.parseDouble(p.getPrice()), p.getTitle(), p.getImage(), p.getSummary(), p.getDetail(), 0);
		}
		return show;
	}

	@Override
	public Result buyProduct(HttpSession session, int id, Result result) throws Exception {
		User user = (User) session.getAttribute("user");
		@SuppressWarnings("unchecked")
		List<Product> products = (List<Product>) (session.getAttribute("productList"));
		for (Product product : products) {
			if (product.getId() == id) {
				if (product.getIsSell() == true) {
					result.isSell(true);
				} else {
					 final TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
					TimeZone.setDefault(tz);
					mapper.buyProduct(id, user.getId(), (long) (Double.parseDouble(product.getPrice()) * 100),
							System.currentTimeMillis());
					result.isSell(false);
				}
			}
		}
		return result;
	}

	@Override
	public Product getSelledProductId(int productId) throws Exception {
		return mapper.productisSell(productId);
	}

	@Override
	public Product getContent(int id) throws Exception {
		return mapper.getContent(id);
	}

	@Override
	public void editProduct(Product product) throws Exception {
		mapper.editContent(product);
	}

	@Override
	public void deleteProduct(int id) throws Exception {
		mapper.deleteContent(id);
	}
}
